package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.OcaFileDao;
import cn.topevery.garden.entity.po.OcaFile;
import cn.topevery.garden.entity.query.OcaFileQuery;
import cn.topevery.garden.entity.vo.OcaFileVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.entity.Value;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class OcaFileDaoImpl extends BaseDaoImpl<OcaFile> implements OcaFileDao {
    @Override
    public <P extends DbCsp> List<OcaFile> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<OcaFileVO> listPage(OcaFileQuery query) {
        String table = "t_oca_file tof";

        String columns = "tof.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tof.c_id desc");
        sqlQueryMate
                .where(true, "tof.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, OcaFileVO.class);
    }

    @Override
    public OcaFile getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public void updateByMainId(Integer mainId) {
        String table = "t_oca_file tof";
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate(table);
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now())
                .where(mainId != null, "c_main_id", ScriptConditionEnum.Eq, mainId);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public List<OcaFile> getListByMainId(Integer id) {
        Value<List<OcaFile>> result = new Value<>();
        String table = "t_oca_file tof";
        String columns = "tof.*";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(id!=null, "c_main_id", ScriptConditionEnum.Eq, id);
        run(sqlQueryMate.getSql(), CommandType.Text, dbCmd -> result.data = dbCmd.executeToList(OcaFile.class));
        return result.data;
    }
}
